package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import liumengbaio.lolzhangyoubao.Activity.InFo.ShowPlayItem;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/15.
 */
public class ShowPlayItemAdapter extends BaseAdapter {
    private String pic_url;
    private Context mContext;
    private ShowPlayItem showPlayItem;

    public ShowPlayItemAdapter(Context mContext, ShowPlayItem showPlayItem) {
        this.mContext = mContext;
        this.showPlayItem = showPlayItem;
    }

    @Override
    public int getCount() {
        return showPlayItem.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return showPlayItem.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ShowPlayItemViewHolder holder = null;
        if (holder == null) {
            holder = new ShowPlayItemViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.show_play_activity_item, null);
            holder.tv_publish_man = (TextView) convertView.findViewById(R.id.tv_publish_man);
            holder.publish_name = (TextView) convertView.findViewById(R.id.publish_name);
            holder.tv_show_play_time = (TextView) convertView.findViewById(R.id.tv_show_play_time);
            holder.tv_show_play_nicker_name = (TextView) convertView.findViewById(R.id.tv_show_play_nicker_name);
            holder.tv_model = (TextView) convertView.findViewById(R.id.tv_model);
            holder.tv_area = (TextView) convertView.findViewById(R.id.tv_area);
            holder.tv_show_play_lolname = (TextView) convertView.findViewById(R.id.tv_show_play_lolname);
            holder.tv_good = (TextView) convertView.findViewById(R.id.tv_good);
            holder.tv_coment_item = (TextView) convertView.findViewById(R.id.tv_coment_item);
            holder.show_play_icon = (ImageView) convertView.findViewById(R.id.show_play_icon);
            holder.iv_show_play_iv1 = (ImageView) convertView.findViewById(R.id.iv_show_play_iv1);
            holder.iv_show_play_iv2 = (ImageView) convertView.findViewById(R.id.iv_show_play_iv2);
            holder.iv_show_play_iv3 = (ImageView) convertView.findViewById(R.id.iv_show_play_iv3);
            holder.iv_show_play_iv4 = (ImageView) convertView.findViewById(R.id.iv_show_play_iv4);
            holder.iv_show_play_iv5 = (ImageView) convertView.findViewById(R.id.iv_show_play_iv5);
            holder.iv_show_play_iv6 = (ImageView) convertView.findViewById(R.id.iv_show_play_iv6);
        } else {
            holder = (ShowPlayItemViewHolder) convertView.getTag();
        }
        holder.tv_show_play_nicker_name.setText(showPlayItem.getData().get(position).getTitle());
        holder.tv_model.setText(showPlayItem.getData().get(position).getMap());
        holder.tv_show_play_time.setText("" + showPlayItem.getData().get(position).getCreated());
        holder.tv_coment_item.setText(showPlayItem.getData().get(position).getComment());
        holder.tv_good.setText(showPlayItem.getData().get(position).getGood());
        holder.publish_name.setText(showPlayItem.getData().get(position).getNickname());
        holder.tv_show_play_lolname.setText(showPlayItem.getData().get(position).getSummoner());
        holder.tv_area.setText(showPlayItem.getData().get(position).getArea());
        String result = readFile("hero/" + showPlayItem.getData().get(position).getRole_id() + ".json");
        Picasso.with(mContext).load(Analytical_data(result)).into(holder.show_play_icon);
        holder.iv_show_play_iv1.setImageBitmap(getBitMap("pic/" + showPlayItem.getData().get(position).getEquips().substring(0, 5) + ".jpg"));
        holder.iv_show_play_iv2.setImageBitmap(getBitMap("pic/" + showPlayItem.getData().get(position).getEquips().substring(6, 11) + ".jpg"));
        holder.iv_show_play_iv3.setImageBitmap(getBitMap("pic/" + showPlayItem.getData().get(position).getEquips().substring(12, 17) + ".jpg"));
        holder.iv_show_play_iv4.setImageBitmap(getBitMap("pic/" + showPlayItem.getData().get(position).getEquips().substring(18, 23) + ".jpg"));
        holder.iv_show_play_iv5.setImageBitmap(getBitMap("pic/" + showPlayItem.getData().get(position).getEquips().substring(24, 29) + ".jpg"));
        holder.iv_show_play_iv6.setImageBitmap(getBitMap("pic/" + showPlayItem.getData().get(position).getEquips().substring(30, 35) + ".jpg"));
        return convertView;
    }

    public class ShowPlayItemViewHolder {
        private TextView tv_publish_man, publish_name, tv_show_play_time,
                tv_show_play_nicker_name, tv_model, tv_area, tv_show_play_lolname,
                tv_good, tv_coment_item;
        private ImageView show_play_icon, iv_show_play_iv1,
                iv_show_play_iv2, iv_show_play_iv3, iv_show_play_iv4,
                iv_show_play_iv5, iv_show_play_iv6;
    }

    public Bitmap getBitMap(String file_name) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //        options.inSampleSize = 2;//图片宽高都为原来的二分之一，即图片为原来的四分之一
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        options.inInputShareable = true;
        AssetManager asm = mContext.getAssets();
        InputStream is;
        Bitmap bitmap = null;
        try {
            is = asm.open(file_name);
            bitmap = BitmapFactory.decodeStream(is
                    , null, options);
            //            mImageView.setImageBitmap(bitmap);
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bitmap;

    }

    /*
    打开本地json数据
     */
    private String readFile(String fileName) {
        String result = "";
        try {
            // 读取assets中的文件，文件内容写入一个输入流里边
            InputStream is = mContext.getAssets().open(fileName);
            // 按行读取
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            // 创建一个临时的字符串来保存每一行的数据
            String line = "";
            // 循环读取文件内容，每次读取一行，如果读取到文件末尾，则读取的内容为null
            while ((line = br.readLine()) != null) {
                result += line;// 将每次读取的内容给最终的result
            }
            // 关闭流,先开的关
            br.close();
            isr.close();
            is.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /*
    解析本地json数据
     */
    private String Analytical_data(String result) {
        try {
            JSONObject object = new JSONObject(result);
            if (object.has("pic_url") && !object.isNull("pic_url")) {
                pic_url = object.getString("pic_url");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return pic_url;
    }
}
